﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using ECS.DAL;
using ECS.DAL.Entity;
using ECS.DAL.Component;

namespace ECS.Framework
{
    /// <summary>
    /// Log Data
    /// </summary>
    public class LogData : ObjectContext
    {
        public string AppId { set; get; }
        public string Device { set; get; }
        public string UserId { set; get; }
        public string LogType { set; get; }
        public string ErrorCode { set; get; }
        public string Process { set; get; }
        public DateTime LogTime { set; get; }
        public string Data1 { set; get; }
        public string Data2 { set; get; }
        public string Data3 { set; get; }
        public string Data4 { set; get; }
        public string Message { set; get; }
    }

    /// <summary>
    /// Log manager
    /// </summary>
    public class LogManager : BaseSQLModel
    {
        private const string SQL_PROC_INSERT_LOG = "usp_LogManager " +
                                                    " @AppId, @Device, @UserId, " +
                                                    "@LogType, @ErrorCode, @Process, @ProcessTime, " +
                                                    "@Data1, @Data2, @Data3, @Data4, @Message";
        private const string SQL_SEARCH = "SELECT [process_id] " +
                                        " ,[app_id],[log_type],[warehouse],[device],[process],[process_datetime] " +
                                        " ,[data_1],[data_2],[data_3],[data_4],[message],[create_date],[create_by] " +
                                        " FROM [t_com_process_log]" +
                                        " ORDER BY [process_datetime],[process_id] ";


        public override string GetInsertSQL(ObjectContext param)
        {
            LogData parameter = (LogData)param;
            StringBuilder sqlCmd = new StringBuilder();
            sqlCmd.Append(SQL_PROC_INSERT_LOG);
            sqlCmd.Replace("@AppId", BaseSQLModel.SqlWithQoute(parameter.AppId));
            sqlCmd.Replace("@Device", BaseSQLModel.SqlWithQoute(parameter.Device));
            sqlCmd.Replace("@UserId", BaseSQLModel.SqlWithQoute(parameter.UserId));
            sqlCmd.Replace("@LogType", BaseSQLModel.SqlWithQoute(parameter.LogType));
            sqlCmd.Replace("@ErrorCode", BaseSQLModel.SqlWithQoute(parameter.ErrorCode));
            sqlCmd.Replace("@Process", BaseSQLModel.SqlWithQoute(parameter.Process));
            sqlCmd.Replace("@ProcessTime", BaseSQLModel.SqlWithQoute(parameter.LogTime.ToString("yyyy-MM-dd")));

            sqlCmd.Replace("@Data1", BaseSQLModel.SqlWithQoute(parameter.Data1));
            sqlCmd.Replace("@Data2", BaseSQLModel.SqlWithQoute(parameter.Data2));
            sqlCmd.Replace("@Data3", BaseSQLModel.SqlWithQoute(parameter.Data3));
            sqlCmd.Replace("@Data4", BaseSQLModel.SqlWithQoute(parameter.Data4));
            sqlCmd.Replace("@Message", BaseSQLModel.SqlWithQoute(parameter.Message));
            return sqlCmd.ToString();
        }
        /// <summary>
        /// input log into database
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool InputLog(ObjectContext param)
        {
            //usp_LogManager  
            //  @in_vchApplicationID = @in_vchApplicationID
            //, @in_vchDevice = @in_vchDevice
            //, @in_vchUserID = @in_vchUserID
            //, @in_vchLogType = 'ERROR','xxx'
            //, @in_vchErrorCode = @out_vchErrorCode
            //, @in_vchProcess = @c_vchObjName
            //, @in_dtProcessDateTime = @out_dtStartTranDate
            //, @in_vchData1 = @v_vchProcessData1
            //, @in_vchData2 = @v_vchProcessData2
            //, @in_vchData3 = @v_vchProcessData3
            //, @in_vchData4 = @v_vchProcessData4
            //, @in_vchMessage = @v_vchMessage

            using (System.Transactions.TransactionScope transaction = BaseSQLModel.CreateTransactionScope())
            {
                using (Entities entities = new Entities())
                {
                    entities.Open();
                    //insert command
                    DbCommand cmd = entities.CreateCommand(GetInsertSQL(param));
                    int m_iRecordChanged = cmd.ExecuteNonQuery();
                    if (m_iRecordChanged < 0)
                    {
                        throw new Exception("Log is not inserted.");
                    }
                }
                transaction.Complete();
            }

            return true;
        }
        /// <summary>
        /// search Log
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<LogData> Search(ObjectContext param)
        {
            /* 
                [process_id] " +
                " ,[app_id],[log_type],[warehouse],[device],[process],[process_datetime] " +
                " ,[data_1],[data_2],[data_3],[data_4],[message],[create_date],[create_by] "
             */
            LogData parameter = (LogData)param;
            List<LogData> items = new List<LogData>();
            using (Entities entities = new Entities())
            {
                entities.Open();
                DbCommand cmd = entities.CreateCommand(SQL_SEARCH);
                DbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    LogData item = new LogData();
                    item.AppId = GetString(1, reader);
                    item.LogType = GetString(2, reader);
                    item.Device = GetString(4, reader);
                    item.Process = GetString(5, reader);
                    item.LogTime = (DateTime)GetDate(6, reader);
                    item.Data1 = GetString(7, reader);
                    item.Data2 = GetString(8, reader);
                    item.Data3 = GetString(9, reader);
                    item.Data4 = GetString(10, reader);
                    item.Message = GetString(11, reader);
                    items.Add(item);
                }
            }
            return items;
        }
    }
}
